<?php
namespace Home\Controller;

use Think\Controller;

class ProductCatesController extends CommonController
{

  protected $this_table = "product_cates";

  public function __construct()
  {
    parent::__construct();

    #TODO：查看权限需要进一步加强
  }

  public function _before_create()
  {
    $this->check_login();
    $this->shops_close();
  }
  public function _before_delete()
  {
    $this->check_login();
    $this->shops_close();
  }

  public function _before_update()
  {
    $this->check_login();
    $this->shops_close();
  }

  public function _before_show()
  {
    $this->check_login();
  }

  public function _before_status()
  {
    $this->check_login();
    $this->shops_close();
  }

  //创建信息
  public function create()
  {
    //必须是商户角色
    if ($this->types != 1) {
      return_data("error", "create.".$this->this_table.".deny", $this->types);
    }

    $model = M($this->this_table);
    $payload = file_get_contents('php://input');
    $data = json_decode($payload, true);

    $data['shop_id'] = $this->shop_id;

    $product_cates_count = $model->where("shop_id={$this->shop_id} and status=1")->count();
    $product_cates_limit = M('shops')->where("id={$this->shop_id}")->getField('product_cates_limit');
    if ($product_cates_limit != 0 && $product_cates_count >= $product_cates_limit) {
      return_data("error", "product_cates.num.limit", $product_cates_limit);
    }

    if (!$data['name']) {
      return_data("error", $this->this_table.".name.blank");
    }

    //查重同个商户 分类名唯一
    //允许删除分类后再创建即允许同名 12.18
    $cate_id = $model->where("shop_id={$this->shop_id} and name='$data[name]' and status=1")->getField('id');
    if ($cate_id) {
      return_data("error", $this->this_table.".name.exist");
    }

    $data['created_at'] = $data['updated_at'] = $this->current_time;
    $model->create($data);
    $id = $model->add();

    if (empty($id)) {
      return_data("error", "create.".$this->this_table.".error");//blank
    } else {
      return_data("success", "create.".$this->this_table.".success", $id);
    }
  }

  //获取信息
  public function show()
  {
    //TODO: 对类型可做统一判断，状态也需要做判断
    if ($this->types != 1) {
      return_data("error", "show.".$this->this_table.".deny");
    }

    $model = M($this->this_table);
    $id = trim(I('id'));

    if (!$id) {
      return_data("error", $this->this_table.".id.blank");
    }

    $where['id'] = $id;
    $where['shop_id'] = $this->shop_id;
    $field = "id, name, orderby";
    $info = $model->field($field)->where($where)->find();

    if (empty($info)) {
      return_data("error", "show.".$this->this_table.".empty");//blank
    } else {
      return_data("success", "show.".$this->this_table.".success", $info);
    }
  }

  //获取列表
  public function index()
  {
    $model = M($this->this_table);
    $field = "*";
    $order = "orderby desc,id desc";
    $shop_id = I('shop_id');

    $shops = M('shops')->field('id')->where(array('id'=>$shop_id,'status'=>2))->find();
    if($shops){
      return_data("error", "shop_id.close.blank");
    }

    if (I('is_business')) {
      $where = array(
        'shop_id' => $shop_id,
        'status' => array('in', '0, 1'));
    } else {
      $where = array(
        'shop_id' => $shop_id,
        'status' => 1);
    }
    if (I('is_platform')) {
      $this->check_login();
      $where = array(
        'app_id' => $this->app_id,
        'shop_id' => 0,
        'status' => 1);
      $list = [];
      $list['platform'] = $model->field($field)->where($where)->order($order)->select();
      $where = array(
        'shop_id' => $this->shop_id,
        'is_platform' => true,
        'status' => 1);
      $list['shop'] = $model->field($field)->where($where)->order($order)->limit(5)->select();
    } else {
      $list = $model->field($field)->where($where)->order($order)->select();
    }

    if (empty($list)) {
      return_data("error", "index.".$this->this_table.".empty");
    } else {
      return_data("success", "index.".$this->this_table.".success", $list);
    }
  }

  //修改
  public function update()
  {
    //必须是商户角色
    if ($this->types != 1) {
      return_data("error", "update.".$this->this_table.".deny");
    }

    $model = M($this->this_table);
    $payload = file_get_contents('php://input');
    $data = json_decode($payload, true);

    $model->create($data);
    $data["updated_at"] = $this->current_time;

    $where['id'] = $data['id'];
    $where['shop_id'] = $this->shop_id;
    $result = $model->where($where)->save($data);

    if ($result) {//$result!==false
      return_data("success", "update.".$this->this_table.".success");
    } else {
      return_data("error", "update.".$this->this_table.".error");
    }
  }

  //删除
  public function delete()
  {
    if ($this->types != 1) {
      return_data("error", "delete.".$this->this_table.".deny");
    }

    $model = M($this->this_table);
    $payload = file_get_contents('php://input');
    $data = json_decode($payload, true);

    $where['id'] = $data['id'];
    $where['shop_id'] = $this->shop_id;
    //TODO: 需要做删除检查
    $result = $model->where($where)->delete();

    if ($result) {
      return_data("success", "delete.".$this->this_table.".success");
    } else {
      return_data("error", "delete.".$this->this_table.".error");
    }
  }

  /**
   * [分类状态更新]
   * @return [type] [description]
   */
  public function status()
  {
    $model = M($this->this_table);

    $payload = file_get_contents('php://input');
    $data = json_decode($payload, true);
    $status_arr = array(0, 1, 2);

    if (!in_array($data['status'], $status_arr)) {
      return_data("error", $this->this_table.".status.error");
    }

    $map = array(
      'id' => $data['id'],
      'shop_id' => $this->shop_id);
    $data['updated_at'] = $this->current_time;
    $result = $model->where($map)->save($data);

    if ($result) {
      return_data("success", "{$this->this_table}.status.success");
    } else {
      return_data("error", "{$this->this_table}.status.error");
    }
  }
}
